#include "Conversion.h"

namespace SUNSHINE
{
	int Conversion::Bin2Dec(const string & s)
	{
		return Conversion::ToDecimal(s, Binary);
	}
	
	int Conversion::Oct2Dec(const string & s)
	{
		return Conversion::ToDecimal(s, Octonary);
	}
	
	int Conversion::Hexa2Dec(const string & s)
	{
		const int BASE = 16;		
		
		static map<char, int> dict;
		Conversion::FillDictionary(dict);
		
		int result = 0;
		string::size_type length = s.size();

		if (0 == length)
		{
			return 0;
		}

		for (string::size_type i = length - 1; i >= 0; --i)
		{
			char c = s[i];
			if (c < '0' 
				|| (c > '9' && c < 'A')
				|| (c > 'F' && c < 'a')
				|| c > 'f')
			{
				return -1;
			}
				
			result += pow(BASE, (length - i - 1)) * dict[c]; 
		}

		return result;
	}
}
